package com.xnyzc.lhy.common.util;

import com.xnyzc.lhy.common.constant.MessageConstant;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期工具
 *
 * @author liuju
 * @create 2019-07-17 13:23
 */
@Slf4j
public class DateUtil {

    private static TimeZone tz = TimeZone.getTimeZone("GMT+8");

    private static TimeZone gmt = TimeZone.getTimeZone("GMT");

    private static final int TOMORROWDAT = 1;

    private static final String YEAR = "1970-01-01 ";
    /**
     * 读取日期的格式
     */
    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();

    /**
     * 获取某个日期的开始时间
     */
    public static Timestamp getDayStartTime(Date d) {
        if (d == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取某个日期的开始时间
     */
    public static Timestamp getDayStartTimeNew(Date d) {
        if (d == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取某个日期的结束时间
     */
    public static Timestamp getDayEndTime(Date d) {
        if (d == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 0);

        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取某个日期的结束时间
     */
    public static Timestamp getDayEndTimeNew(Date d) {
        if (d == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 0);

        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取月开始时间
     */
    public static Date getDayOfMonthStart(Date date) {
        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    /**
     * 获取月结束时间
     */
    public static Date getDayOfMonthEnd(Date date) {
        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 0);

        return calendar.getTime();
    }

    /**
     * 获取当年的开始时间戳
     *
     * @param date 当前日期
     * @return
     */
    public static Date getYearStartTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        calendar.add(Calendar.YEAR, 0);
        calendar.add(Calendar.DATE, 0);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Date(calendar.getTimeInMillis());
    }

    /**
     * 获取当年的最后时间戳
     *
     * @param date 日期
     * @return
     */
    public static Date getYearEndTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        int year = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return new Date(calendar.getTimeInMillis());
    }

    /**
     * 日期增加月数
     */
    public static Date dateAddDays(Date date, int days) {
        if (date == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);

        return calendar.getTime();
    }

    /**
     * 日期增加天数
     */
    public static Date dateAddDay(Date date, int days) {
        if (date == null) {
            return null;
        }

        String month = convertToStringCustom(date, "MM");

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        calendar.add(Calendar.DATE, days);

        return calendar.getTime();
    }

    /**
     * 增加月份
     *
     * @param date   当前日期
     * @param aMount 增加月份数
     * @return
     */
    public static Date addMonth(Date date, int aMount) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MONTH, aMount);
        return rightNow.getTime();
    }


    /**
     * 时间增加分钟数
     */
    public static Date dateAddMinutes(Date d, int minutes) {
        if (d == null) {
            return null;
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.add(Calendar.MINUTE, minutes);

        return calendar.getTime();
    }

    /**
     * 获取年份
     *
     * @param millisecond
     * @return
     * @author TaoNingBo
     */
    public static String getDateTimeYear(long millisecond) {
        if (millisecond == 0) {
            millisecond = getCurMilli();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        Calendar calendar = getCalendar();
        calendar.setTimeInMillis(millisecond);
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static long getCurMilli() {
        long millisecond = 0;
        TimeZone.setDefault(tz);
        Calendar cal = Calendar.getInstance();
        millisecond = cal.getTimeInMillis();
        return millisecond;
    }

    /**
     * 得到系统Calendar日期
     *
     * @return
     */
    public static Calendar getCalendar() {
        TimeZone.setDefault(tz);
        Calendar cal = Calendar.getInstance();
        return cal;
    }


    /**
     * 时间格式必须
     * 2017-01-2 19:20:00
     *
     * @return
     */
    public static Date dateAdd(Date startTime, Long endTime) {
        if (startTime != null) {
            long time1 = startTime.getTime();
            time1 = time1 + endTime;
            Date date = new Date(time1);
            return date;
        }
        return null;
    }

    /**
     * 时间相减
     *
     * @param startTime
     * @param endTime
     * @return 以分钟
     */
    public static double dateSubtract(Long startTime, Long endTime) {
        double nd = 1000 * 24 * 60 * 60;
        double nh = 1000 * 60 * 60;
        double nm = 1000 * 60;
        double ns = 1000;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        double diff = endTime - startTime;
        // 计算差多少天
        double day = diff / nd;
        // 计算差多少小时
        double hour = diff % nd / nh;
        // 计算差多少分钟
        double min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        double sec = diff % nd % nh % nm / ns;
        return min;
    }

    public static double dateSubtractDay(Long startTime, Long endTime) {
        double nd = 1000 * 24 * 60 * 60;
        // 获得两个时间的毫秒时间差异
        double diff = endTime - startTime;
        // 计算差多少天
        return diff / nd;
    }

    /**
     * 时间相减 (一分钟为单位) 开始时间 - 结束时间
     */
    public static double dateSubtractBuffer(Long startTime, Long endTime) {
        double newTime = startTime - endTime;
        return newTime / (1000 * 60);
    }

    /**
     * 时间相减
     *
     * @return 以分钟
     */
    public static Long dateSubtractLong(Date sdate, Date edate) {
        long between = (edate.getTime() - sdate.getTime()) / 1000;
        long min = between / 60;
        return min;
    }

    /**
     * 获取当日日期
     *
     * @return
     * @author TaoNingBo
     */
    public static String getDateDay() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = getCalendar();
        return dateFormat.format(calendar.getTime());
    }

    /**
     * 获取中文日期
     *
     * @param date 时间
     * @return
     */
    public static String getChineseDate(Date date) throws ParseException {
        String dateChinese = "";
        SimpleDateFormat dateFormat = new SimpleDateFormat(" HH:mm");
        //如果时间为为空返回当前时间中文显示
        if (CheckUtil.objIsEmpty(date)) {
            date = new Date();
        }
        if (isNow(date)) {
            dateChinese = MessageConstant.TODAY + dateFormat.format(date);
        } else {
            if (IsTomorrowday(date)) {
                dateChinese = MessageConstant.TOMORROW + dateFormat.format(date);
            } else {
                dateChinese = getDay(date) + MessageConstant.DAY + dateFormat.format(date);
            }
        }
        return dateChinese;
    }

    /**
     * 判断时间是不是今天
     *
     * @param date
     * @return 是返回true，不是返回false
     */
    private static boolean isNow(Date date) {
        //当前时间
        Date now = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
        //获取今天的日期
        String nowDay = sf.format(now);
        //对比的时间
        String day = sf.format(date);

        return day.equals(nowDay);

    }

    /**
     * 判断是不是明天
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static boolean IsTomorrowday(Date date) throws ParseException {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == TOMORROWDAT) {
                return true;
            }
        }
        return false;
    }


    /**
     * 读取日期的格式
     */
    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA));
        }
        return DateLocal.get();
    }

    /**
     * 获取时间当前天
     *
     * @param date
     * @return
     */
    public static String getDay(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
        if (CheckUtil.objIsEmpty(date)) {
            date = new Date();
        }
        return dateFormat.format(date);
    }


    /**
     * 判断时间段是否是被包含
     *
     * @param startTime        开始时间段 09:00 格式
     * @param endTime          结束时间段 18 : 00 格式
     * @param currentStartTime 当前时间
     * @return
     */
    public static boolean isRange(String startTime, String endTime, Date currentStartTime) {
        try {
            SimpleDateFormat spf = new SimpleDateFormat("HH:mm");
            SimpleDateFormat spfYear = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//            spf.setTimeZone(gmt);
            spfYear.setTimeZone(gmt);
            Date startDate = spfYear.parse(YEAR + startTime);
            Date endDate = spfYear.parse(YEAR + endTime);
            Date nowTime = spfYear.parse(YEAR + spf.format(currentStartTime));
            if (nowTime.getTime() == startDate.getTime()
                    || nowTime.getTime() == endDate.getTime()) {
                System.out.println(true);
            }
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);

            Calendar begin = Calendar.getInstance();
            begin.setTime(startDate);

            Calendar end = Calendar.getInstance();
            end.setTime(endDate);
            //当前时间大于开始时间，并且小于结束时间
            if (date.after(begin) && date.before(end)) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            log.info("时间转换错误");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断时间段是否是被包含
     *
     * @param startTime 开始时间段 09:00:00 格式
     * @param endTime   结束时间段 18:00:00 格式
     * @return
     */
    public static boolean timeIsRange(String startTime, String endTime) {
        Date currentStartTime = new Date();
        try {
            SimpleDateFormat spf = new SimpleDateFormat("HH:mm:ss");
            SimpleDateFormat spfYear = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//            spf.setTimeZone(gmt);
            spfYear.setTimeZone(gmt);
            Date startDate = spfYear.parse(YEAR + startTime);
            Date endDate = spfYear.parse(YEAR + endTime);
            Date nowTime = spfYear.parse(YEAR + spf.format(currentStartTime));
            Calendar date = Calendar.getInstance();
            date.setTime(nowTime);
            Calendar begin = Calendar.getInstance();
            begin.setTime(startDate);
            Calendar end = Calendar.getInstance();
            end.setTime(endDate);
            //当前时间大于开始时间，并且小于结束时间
            if (date.after(begin) && date.before(end)) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            log.info("时间转换错误");
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @param startTime
     * @param endTime
     * @param currentTime
     * @return
     */
    public static BigDecimal isInDate(String startTime, String endTime, String currentTime, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            Date isRangeStartTime = sdf.parse(startTime);
            Date isRangeEndTime = sdf.parse(endTime); //自定义时间
            Date currentStartTime = sdf.parse(currentTime);
            Date currentEndTime = sdf.parse(endDate);

            Calendar currentStartCalendar = Calendar.getInstance();
            currentStartCalendar.setTime(currentStartTime);

            Calendar currentEndCalendar = Calendar.getInstance();
            currentEndCalendar.setTime(currentEndTime);

            Calendar end = Calendar.getInstance();
            end.setTime(isRangeEndTime);

            Calendar start = Calendar.getInstance();
            start.setTime(isRangeStartTime);

            BigDecimal result = BigDecimal.valueOf(0.0);

            if (currentStartTime.after(currentEndTime)) {
                result = result.add(isInDate(startTime, endTime, currentTime, "23:59"));
                result = result.add(isInDate(startTime, endTime, "00:00", endDate));
                return result;
            } else {
                //如果订单开始时间大于结束时间，不需要计算
                if (currentStartCalendar.after(end)) {
                    return result;
                }

                Long millisecond = 0L;
                //订单结束时间大于特殊结束时间
                if (currentEndCalendar.after(end)) {
                    //订单开始时间如果大于特殊开始时间，使用订单开始时间进行计算
                    if (currentStartCalendar.after(start)) {
                        start = currentStartCalendar;

                    }
                    millisecond = end.getTimeInMillis() - start.getTimeInMillis();
                }
                //订单结束时间小于特殊结束时间
                else {
                    end = currentEndCalendar;
                    if (currentStartCalendar.after(start)) {
                        start = currentStartCalendar;
                    }
                    millisecond = end.getTimeInMillis() - start.getTimeInMillis();
                }
                //判断毫秒值是否正确
                if (millisecond > 0) {
                    result = result.add(new BigDecimal(millisecond / 1000 / 60));
                }
                return result;
            }
        } catch (ParseException e) {
            System.out.println("时间转换失败");
        }
        return new BigDecimal(0);
    }

    /**
     * 判断是否在一个时间段内
     *
     * @param nowTime
     * @param beginTimeStr
     * @param endTimeStr
     * @return
     */
    public static boolean belongCalendar(Date nowTime, String beginTimeStr, String endTimeStr) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        Date beginTime = null;
        Date endTime = null;
        try {
            beginTime = df.parse(beginTimeStr);
            endTime = df.parse(endTimeStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static Map<String, Date> inDateGetStartAndEnd(String startTime, String endTime, String currentTime, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Map<String, Date> dateMap = new HashMap<>();
        try {
            Date isRangeStartTime = sdf.parse(getStringToYYYYMMdd() + startTime);
            Date isRangeEndTime = sdf.parse(getStringToYYYYMMdd() + endTime); //自定义时间
            Date currentStartTime = sdf.parse(getStringToYYYYMMdd() + currentTime);
            Date currentEndTime = sdf.parse(getStringToYYYYMMdd() + endDate);
            // 当前开始时间
            Calendar currentStartCalendar = Calendar.getInstance();
            currentStartCalendar.setTime(currentStartTime);
            // 当前结束时间
            Calendar currentEndCalendar = Calendar.getInstance();
            currentEndCalendar.setTime(currentEndTime);
            // 特殊时段结束时间
            Calendar end = Calendar.getInstance();
            end.setTime(isRangeEndTime);
            // 特殊时段开始时间
            Calendar start = Calendar.getInstance();
            start.setTime(isRangeStartTime);

            if (currentStartTime.after(currentEndTime)) {
                Map<String, Date> stringDateMap = inDateGetStartAndEnd(startTime, endTime, currentTime, "23:59");
                if (CheckUtil.mapIsNotEmpty(stringDateMap)) {
                    dateMap.putAll(stringDateMap);
                }
                stringDateMap = inDateGetStartAndEnd(startTime, endTime, "00:00", endDate);
                if (CheckUtil.mapIsNotEmpty(stringDateMap)) {
                    dateMap.putAll(stringDateMap);
                }
            } else {
                Long millisecond = 0L;
                //订单结束时间大于特殊结束时间
                if (currentEndCalendar.after(end)) {
                    //订单开始时间如果大于特殊开始时间，使用订单开始时间进行计算
                    if (currentStartCalendar.after(start)) {
                        start = currentStartCalendar;

                    }
                    millisecond = end.getTimeInMillis() - start.getTimeInMillis();
                    //判断毫秒值是否正确
                    if (millisecond > 0) {

                        dateMap.put("start", start.getTime());
                        dateMap.put("end", end.getTime());
                        return dateMap;
                    }
                }
                //订单结束时间小于特殊结束时间
                else {
                    end = currentEndCalendar;
                    if (currentStartCalendar.after(start)) {
                        start = currentStartCalendar;
                    }
                    millisecond = end.getTimeInMillis() - start.getTimeInMillis();
                    //判断毫秒值是否正确
                    if (millisecond > 0) {
                        dateMap.put("start", start.getTime());
                        dateMap.put("end", end.getTime());
                        return dateMap;
                    }
                }

            }
        } catch (ParseException e) {
            System.out.println("时间转换失败");
        }
        return dateMap;
    }

    /**
     * 分钟转换秒
     *
     * @return
     */
    public static BigDecimal minutesConversionSeconds(BigDecimal minutes) {
        BigDecimal seconds = minutes.multiply(new BigDecimal(60));
        return seconds;
    }

    /**
     * 时间转字符串
     */
    public static String convertDateToString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    /**
     * 时间转字符串 (月日)
     */
    public static String convertDateToStringMonthDay(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
        return simpleDateFormat.format(date);
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDateShort(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static Date getDateStringShort(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try{
           date = format.parse(dateStr);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date;
    }

    public static String getStringDateHourShort(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static Date getDateHourShort(long currentTime) {
        Date cDate = new Date(currentTime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH");
        String dateString = formatter.format(cDate);
        Date date = null;
        try{
            date = formatter.parse(dateString);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date;
    }

    public static Date getDateDayShort(long currentTime) {
        Date cDate = new Date(currentTime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(cDate);
        Date date = null;
        try{
            date = formatter.parse(dateString);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date;
    }

    public static Date getDateHourStringShort(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH");
        Date date = null;
        try{
            date = format.parse(dateStr);
        }catch (ParseException e){
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 获取现在时间的上一天
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDateShortUp(Date currentTime) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    /**
     * 获取现在时间的下一天
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDateShortDow(Date currentTime) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.DAY_OF_MONTH, +1);

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(calendar.getTime());
        return dateString;
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式MM-dd
     */
    public static String getStringToDayMMdd(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式MM-dd
     */
    public static String getStringToDayYYYYMMdd(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyMM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间的上一天
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringToDayMMddUp(Date currentTime) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentTime);
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
        String dateString = formatter.format(calendar.getTime());

        return dateString;
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringToDayYYMMdd(Date currentTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 转换字符串（自定义类型）
     */
    public static String convertToStringCustom(Date date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     * 字符串转Date
     */
    public static Date convertStringToDate(String date) {
        return convertStringToDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 字符串转Date
     */
    public static Date convertStringToDate(String date, String pattern) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        try {
            return simpleDateFormat.parse(date);
        } catch (ParseException e) {
            log.error("日期转换错误");
            return new Date();
        }
    }

    /**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /*
     * 获取年月日,重点保留后面空格不要清除
     *
     * @return
     */
    public static String getStringToYYYYMMdd() {
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        String year = now.get(Calendar.YEAR) + "-" + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DAY_OF_MONTH) + " ";
        return year;
    }

    /**
     * 获取本周一时间戳
     * @return
     */
    public static long getMondayTime(){
        Calendar cal =Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 00);
        cal.set(Calendar.MINUTE, 00);
        cal.set(Calendar.SECOND, 00);
        return cal.getTime().getTime();
    }
    /**
     * 获取本周日时间戳
     * @return
     */
    public static long getSundayTime(){
        Calendar cal =Calendar.getInstance();
        //这种输出的是上个星期周日的日期，因为老外那边把周日当成第一天
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        //增加一个星期，才是我们中国人理解的本周日的日期
        cal.add(Calendar.WEEK_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        ////获取本周日的时间戳
        return cal.getTime().getTime();
    }

    public static long getStringDateTime(String time,String pattern) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = format.parse(time);
        return date.getTime();
    }

    /**
     * 将date转成ISODate 以便mongo识别
     * @param dateStr
     * @return
     */
    public static Date dateToISODate(Date dateStr) {
        Date parse = null;
        try {
            // 解析字符串时间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            parse = format.parse(format.format(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    /**
     * 获取这天的时份
     * @param date
     * @return
     */
    public static int getTodayHour(Date date){
        Calendar calendar =Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

}
